package org.zyl.util.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作工具
 * @author yzzhouyalei@foxmail.com
 * @date 2017-01-02 14:47:02
 */
public class StringUtil {
	
	public static String cutStr(String value,int len){
		if (!ValidateUtil.isNotBlank(value)) {
			return value;
		}
		
		StringBuffer sb = new StringBuffer();
		int charlen = 0;
		int strlength = value.length();
		for (int i = 0; i < strlength; i++) {
			int asciicode = value.codePointAt(i);
			if (asciicode>=0&&asciicode<=255) {
				charlen+=1;
			}else{
				charlen+=2;
			}
			
			if (charlen<=len) {
				sb.append(value.charAt(i));
			}
		}
		return sb.toString();
	}
	
	public static String filterSpecialStr(String temp){
		if (!ValidateUtil.isNotBlank(temp)) {
			return temp;
		}
		temp = temp.replaceAll("'", "");
		temp = temp.replaceAll("\"", "");
		temp = temp.replaceAll("#", "");
		temp = temp.replaceAll("-", "");
		return temp;
	}
	public static String filterSpecial_CHINESE_ENGLISH_NUMBER(String character) {
		if (!ValidateUtil.isNotBlank(character)) {
			return character;
		}
		
		character = character.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5]+", "");
		return character;
	}

	
	/**
	 * 去除所有空格
	 */
	public static String replaceBlank(String str) {
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(str);
		String after = m.replaceAll("");

		return after;
	}

	/**
	 * 判断字符串格式（共8位，前两位为字母，后6位为数字，如:AA100001）
	 */
	public static boolean checkLetterAndNumeral(String s) {
		boolean a = false;
		/*
		 * Regexp匹配模式
		 */
		String userName = "^[a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9][0-9]$";
		Pattern pattern = Pattern.compile(userName);

		Matcher matcher = pattern.matcher(s);
		a = matcher.matches();
		return a;
	}

	/**
	 * 判断字符串是否为整数（包括正数与负数）
	 */
	public static boolean isInt(String str) {
		String intRegex = "^-?\\d+$";
		if (null != str) {
			return str.matches(intRegex);
		} else {
			return false;
		}
	}

	/**
	 * 将以逗号字符串转换为一个String 数组
	 */
	public static String[] convertStrToArray(String str) {
		String[] strArray = null;
		// 兼容中文逗号
		String subStr = "，";
		if (str.contains(subStr)) {
			str = str.replace(subStr, ",");
		}
		strArray = str.split(","); // 拆分字符为"," ,然后把结果交给数组strArray
		return strArray;
	}

	public static String toChinese(String strvalue) {
		try {
			if (strvalue == null) {
				return "";
			} else {
				strvalue = new String(strvalue.getBytes("ISO8859_1"), "UTF-8")
						.trim();
				return strvalue;
			}
		} catch (Exception e) {
			return "";
		}
	}
	/**
	 * 首字母小写
	 * 
	 * @author yzzhouyalei@foxmail.com
	 * @time 2019-01-10 21:50:53
	 */
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	/**
	 * 首字母大写
	 * 
	 * @author yzzhouyalei@foxmail.com
	 * @time 2019-01-10 21:50:50
	 */
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}
}